//plugin definition
(function($) { 
$.fn.quickselect = function(options)
{
	var opts = $.extend({}, $.fn.quickselect.defaults, options);
	opts.controlId = $(this).attr("id");
	if(opts.selectionLabelId=="") opts.selectionLabelId = opts.controlId +"_label";
	if(opts.selectionsId=="") opts.selectionsId = opts.controlId +"_selections";
	if(opts.innerContainerId=="") opts.innerContainerId = opts.controlId +"_innerContainer";
	if(opts.outerContainerId=="") opts.outerContainerId = opts.controlId +"_outerContainer";
	if(opts.deleteButtonId=="") opts.deleteButtonId = opts.controlId +"_deleteButton";
	// plugin implimentation code goes here
	$(this)
	.click(clearSelection)
	.change(completeSelection)
	.addClass(opts.controlClass)
	.mouseover(showSelections)
	.wrap('<div id="'+opts.outerContainerId+'" class="'+opts.outerContainerClass+'"></div>')
	.before('<input type="text" id="'+opts.selectionLabelId+'" class ="'+opts.selectionLabelClass+'" value="'+opts.label+'"/>')
	.after('<ul id="'+opts.selectionsId+'" class="'+opts.selectionClass+' '+opts.hiddenClass+'"></ul>')
	.parent(0).mouseleave(hideSelections);
	$('#'+opts.controlId)
	.wrap('<div id="'+opts.innerContainerId+'" class="'+opts.innerContainerClass+'"></div>')
	.after('<input type="button" id="'+opts.deleteButtonId+'" class="'+opts.deleteButtonClass+'" value = "X"/>');
	$('#' +opts.deleteButtonId).hover(function(){ $(this).addClass("ui-state-hover");},function(){ $(this).removeClass("ui-state-hover");})
	$('#'+opts.deleteButtonId).click(deleteSelection);
	populateSelections();
	return $(this);

	//private methods
	
	function hideSelections()
	{
		$('#'+opts.selectionsId).addClass(opts.hiddenClass);
	}
	
	function deleteSelection()
	{
		var itemToRemove = $('#'+opts.controlId).attr("value");
		$('#'+opts.controlId).attr("value","")
		var j = 0;
		while (j < opts.selections.length) 
		{
			if (opts.selections[j] == itemToRemove) 
			{
				opts.selections.splice(j, 1);
			} 
			else { j++; }
		}
		populateSelections();
	}
	

	function showSelections()
	{
			 $('#'+opts.selectionsId).removeClass(opts.hiddenClass);
			 if (opts.onShowSelections !="") 
			 {
				eval(opts.onShowSelections);
			 }
	}
	
	function makeSelection()
	{
		$('#'+opts.controlId).attr("value",$(this).text());
		hideSelections();
	}
	
	function clearSelection()
	{
		$('#'+opts.controlId).attr("value","");
	}
	
	function completeSelection()
	{
		var selection = $('#'+opts.controlId).attr("value");
		if ($.inArray(selection, opts.selections)==-1)
		{
			opts.selections.push(selection);
			populateSelections();
		}
	}
		
	function populateSelections()
	{
		$('#'+opts.selectionsId).children().remove();
		opts.selections.sort();
		for (var dataId in opts.selections)
		{
			$('#'+opts.selectionsId).append('<li class = "'+opts.selectionItemClass+'">'+opts.selections[dataId]+'</li>');
		}
		$('#'+opts.selectionsId).children().click(makeSelection);
		$('#'+opts.selectionsId).children().addClass(opts.selectionItemClass);
		$('#'+opts.selectionsId).children().hover(function() {$(this).addClass(opts.selectionItemHoverClass);}, function() {$(this).removeClass(opts.selectionItemHoverClass);});
		$('#' + opts.controlId).trigger('selectionsPopulated');
	}
};

$.fn.quickselect.defaults =
{
	controlId: "",
	selections: "",
	limitToList: false,
	selectionsId: "",
	innerContainerId: "",
	outerContainerId: "",
	controlClass: "ui-state-default",
	innerContainerClass: "",
	outerContainerClass: "ui-widget ui-widget-quickselect",
	hiddenClass: "ui-helper-hidden",
	disabledClass: "ui-state-disabled",
	selectionClass: "quickSelect_ul",
	selectionItemClass: "ui-state-default quickSelect_li",
	selectionItemHoverClass: "ui-state-focus",
	deleteButtonId : "",
	deleteButtonClass: "ui-state-default fg-button  quickSelect_deleteButton",
	label: "Item",
	selectionLabelId: "",
	selectionLabelClass: "quickSelect_label ui-widget ui-widget-content",
	enabled: true,
	onShowSelections: ""
};


})(jQuery);

